<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
     <!--   <el-col :span="12">
            <el-form-item label="业务主表ID" prop="handwritingTime.id"
                :rules="[
                 ]">
              <el-input v-model="inputForm.handwritingTime.id" placeholder="请填写业务主表ID"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label=" 开始时间" prop="startTime"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.startTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间"
                      :picker-options="pickerOptionsStart">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="结束时间" prop="endTime"
                :rules="[
                 ]">
                <el-date-picker
                      v-model="inputForm.endTime"
                      type="datetime"
                      style="width: 100%;"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间"
                      :picker-options="pickerOptionsEnd">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        inputForm: {
          id: '',
          handwritingTime: {
            id: ''
          },
          startTime: '',
          endTime: '',
          remarks: ''
        },
        // 限制时间范围
          // 活动开始时间不能选择今天之前的时间 今天之前的时间设置禁止选择状态
        pickerOptionsStart: {
          disabledDate: (time) => {
            if (this.inputForm.endTime) {
              return (
                 // 设置选择今天以及今天之后的日期   8.64e7就是一天的时间戳
                time.getTime() < Date.now() - 8.64e7 ||
                time.getTime() > this.inputForm.endTime
              )
            }
            return time.getTime() < Date.now() - 8.64e7
          }
        },
        // 活动结束时间不能选择活动开始的之前的时间
        pickerOptionsEnd: {
          disabledDate: (time) => {
            return (
              // 设置选择今天以及今天之后的日期
              time.getTime() < Date.now() - 8.64e7 ||
              time.getTime() < new Date(this.inputForm.startTime).getTime()
            )
          }
        }
      }
    },
    components: {
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'add') {
          this.title = `新建暂停时间`
        } else if (method === 'edit') {
          this.title = '修改暂停时间'
        } else if (method === 'view') {
          this.title = '查看暂停时间'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>
